import * as bajaService from '@/services/bajaService';
import { CORE_JAR_PREFIX } from '@/utils/constants';
import memoryUtils from '@/utils/memoryUtils';

export default {
  namespace: 'subModule',
  state: {
    $handle: '',
    $typeSpec: CORE_JAR_PREFIX + ':CommonModule',
    title: '',
    nIcon: '',
    moduleType: 'Tree Menu',
    pagePath: '/subSystem',
    url: '',
    navOrd: '',
    treeMenus: [],
    selectedTreeMenu: null
  },
  reducers: {
    setData(state, { payload }) {
      return { ...state, ...payload };
    },
    setSelectedTreeMenu(state, {payload: {selectedTreeMenu}}) {
      return { ...state, selectedTreeMenu};
    }
  },
  effects: {
    *fetch({ payload: {subModuleOrd} }, { call, put }) {
      let $handle='', $typeSpec='', title='', nIcon='', moduleType='', pagePath='', url='', navOrd='', treeMenus=[];
      const subModule = yield call(bajaService.Ord, subModuleOrd);
      if(subModule) {
        $handle = subModule.$handle;
        $typeSpec = subModule.getType().$typeSpec;
        title = subModule.getTitle();
        nIcon = subModule.getNIcon();
        navOrd = subModule.getNavOrd().toString();
        if(!$typeSpec.endsWith('ModuleGroup')) {
          const { $ordinal } =  subModule.getModuleType();
          moduleType = memoryUtils.MODULE_TYPE[$ordinal];
          pagePath = subModule.getPagePath();
          url = subModule.getUrl();
          if(moduleType === 'HISTORY'||moduleType === 'ALARM'||moduleType === 'TREE_MENU'||moduleType === 'PAGE') {
            const treeMenuRoot = yield call(bajaService.Ord, navOrd+'/treeMenuRoot');
            if(treeMenuRoot) {
              memoryUtils.TREEMENU_MODE = {};
              memoryUtils.TREEMENUITEM_TYPE = {};
              memoryUtils.ENERGYCATEGORY_TYPE = {};
              const treeMenuMode = treeMenuRoot.getTreeMenuModeExpose();
              const obj = JSON.parse(treeMenuMode);
              for(let key in obj) {
                memoryUtils.TREEMENU_MODE[obj[key]] = key;
              }
              const mode =  memoryUtils.TREEMENU_MODE[treeMenuRoot.getMode().$ordinal];
              treeMenus = yield call(bajaService.fetchNavChildren, treeMenuRoot);
              treeMenus = yield call(bajaService.fetchTreeMenus, {treeMenus, mode});
              if(!treeMenus) {
                treeMenus = []
              }
            }
          }
        }
      }
      yield put({ type: 'setData', payload: {
        $handle, $typeSpec, title, nIcon, moduleType, pagePath, url, navOrd, treeMenus, selectedTreeMenu:null
      }});
    },
    *clearAll({ _ }, { put }) {
      yield put({ type: 'setData', payload: {
        $handle: '',
        $typeSpec: CORE_JAR_PREFIX + ':CommonModule',
        title: '',
        nIcon: '',
        moduleType: 'Tree Menu',
        pagePath: '/subSystem',
        url:'',
        navOrd: '',
        treeMenus: [],
        selectedTreeMenu: null
      }});
    },
    *selectTreeMenu({ payload: {selectedTreeMenu} }, { put }) {
      yield put({ type: 'setSelectedTreeMenu', payload: {selectedTreeMenu}});
    }
  }
}
